<?php
namespace Home\Controller;

use Think\Controller;

class ToolController extends BaseController
{

    /**
     * 省级联动AJAX
     */
    public function getCityAjax(){
        $pid=intval($_POST['pid']);
        $this->ajaxReturn(getcity($pid));
    }
    public function getxianAjax(){
        $pid=intval($_POST['pid']);
        $this->ajaxReturn(getxian($pid));
    }
    /**
     * 省级联动AJAX
     */
    public function getCategoryAjax(){
        $pid=intval($_POST['pid']);
        $this->ajaxReturn(getcategory($pid));
    }

    // 获取所有省
    public function getProvince(){
        $this->ajaxSuccess(array("province" => getCity(0)));
    }

    // 上传图片
    public function uploadImg()
    {
        $res = D('Upload')->imageUpload();
        print_r($res);
    }

    public function getCode(){
        ob_clean();
        $config =    array(
            'fontSize'    =>    12,    // 验证码字体大小
            'length'      =>    4,     // 验证码位数
            'useNoise'    =>    true, // 关闭验证码杂点
            'expire'      =>    120,    //验证码有效期
            'useImgBg'     =>  false
        );
        $Verify = new \Think\Verify($config);
        $Verify->entry();
    }

    /*发送短信验证码*/
    public function getnum()
    {
        // 获取配置
        $sms = getConfig('sms');
        $url = $sms['url'];
        $key = $sms['key'];
        $tplId = $sms['tplId'];

        $code = rand(100000, 999999);
        $_SESSION["code"] = $code;
        $_SESSION["codetime"] = time();
        $sendUrl = $url; //短信接口的URL

        $smsConf = array(
            'key' => $key, //您申请的APPKEY
            'mobile' => $_POST['mobile'], //接受短信的用户手机号码
            'tpl_id' => $tplId, //您申请的短信模板ID，根据实际情况修改
            'tpl_value' => '#code#=' . $code . '&#company#=乐荟云商' //您设置的模板变量，根据实际情况修改
        );

        $content = juhecurl($sendUrl, $smsConf, 1); //请求发送短信

        if ($content) {
            $result = json_decode($content, true);
            $error_code = $result['error_code'];
            if ($error_code == 0) {
                //状态为0，说明短信发送成功
                //echo "短信发送成功,短信ID：".$result['result']['sid'];
                echo 1;
            } else {
                //状态非0，说明失败
                $msg = $result['reason'];
                echo "短信发送失败(" . $error_code . ")：" . $msg;
            }
        } else {
            //返回内容异常，以下可根据业务逻辑自行修改
            echo "请求发送短信失败";
        }
    }
}